
$(function(){
	var cols = [
		{title:'主键', name:'id', align: 'center',hidden:true},
		{title:'所属应用', name:'quartzApp', align: 'center'},
		{title:'任务名称(英文)', name:'quartzJobname', align: 'center'},
		{title:'任务名称(中文)', name:'quartzJobnameCn', align: 'center'},
		{ title:'cron表达式', name:'quartzCronexp' , align:'center' },
		{ title:'cron表达式说明', name:'quartzCronexpNote' , align:'center' },
		{ title:'任务url', name:'quartzJoburl' , align:'center',width:300 },
		{ title:'是否启用', name:'quartzEnable' , align:'center' ,
			renderer:function(val,item,rowIndex){
				if(val=='1'){
					return '启用';
				}else{
					return '禁用';
				}
			}
		},
		{ title:'操作',name:'id',align:'center',width:200,renderer:function(val,item,rowIndex){
			var id = item.id;
			var opt_str = '';
			if(item.quartzEnable=='1'){
				opt_str = '<a href="#" onclick="setEnable('+rowIndex+')">禁用</a>&nbsp;&nbsp;|';
			}else{
				opt_str = '<a href="#" onclick="setEnable('+rowIndex+')">启用</a>&nbsp;&nbsp;|';
			}
			opt_str += '<a href="#" onclick="execJob('+rowIndex+')">立即执行</a>&nbsp;&nbsp;|';
			return opt_str+'<a href="quartzController/goQuartzDetail.do?id='+id+'">修改</a>'+
				'&nbsp;&nbsp;|&nbsp;&nbsp;'+
				'<a href="#" onclick="removeRow('+rowIndex+')">删除</a>';
		}}
	];
	var grid=$('#datagrid').mmGrid({
		cols: cols,
		url: 'quartzController/queryQuartzList.do',
		method: 'get',
		checkCol:true,
		plugins:[
			$('#paginator').mmPaginator({
				style: 'plain',
				totalCountName: 'totalCount',
				page: 1,
				pageParamName: 'pageNo',
				limitParamName: 'pageSize',
				limitLabel: '每页{0}条',
				totalCountLabel: '共<span>{0}</span>条记录',
				limit: undefined,
				limitList: [10,20, 30, 50]
			})
		],
		showBackboard:false,
		fullWidthRows:true,
		checkCol:false,
		height:'auto',
		noDataText:'没有数据。',
		multiSelect:true
	});
	function refreshCondition(){
		var obj = new Object();
		if( $.trim( $('#quartzApp').val() ) != '' ){
			obj.quartzApp = $('#quartzApp').val();
		} else{
			delete grid.opts.params.quartzApp;
		}
		if( $.trim( $('#quartzJobname').val() ) != '' ){
			obj.quartzJobname = $('#quartzJobname').val();
		} else{
			delete grid.opts.params.quartzJobname;
		}
		if( $.trim( $('#quartzJobnameCn').val() ) != '' ){
			obj.quartzJobnameCn = $('#quartzJobnameCn').val();
		} else{
			delete grid.opts.params.quartzJobnameCn;
		}


		$.extend(grid.opts.params,obj);
	}
	$('#empty_link').click(function(){
		$('#quartzApp').val('');
		$('#quartzJobname').val('');
		$('#quartzJobnameCn').val('');
	});

	$('#query_link').click(function(){
		refreshCondition();
		grid.resetLoad();
	});

	$('#add_link').click(function(){
		location.href = "quartzController/goQuartzDetail.do";
	});
})


function execJob(idx){
	var row = $('#datagrid').mmGrid("row",idx);
	var id = row.id;
	$.ajax({
		url:'quartzController/execJob.do',
		data:{id:id},
		success:function(data){
			if(data=="true"){
				layer.alert("执行成功");
			}else if(data=="false"){
				layer.alert("执行失败");
			}
		}
	});
}

function removeRow(idx){
	var row = $('#datagrid').mmGrid("row",idx);
	var id = row.id;
	layer.confirm('确认删除所选信息?',{btn:['确定','关闭']},function(){
		$.post("quartzController/delQuartz.do?id="+id,function(d){
			var sRtn = $.parseJSON(d);
			layer.alert(sRtn.success);
			if(sRtn.success){
				$('#query_link').click();
			}
		});
	});
}

function setEnable(idx){
	var row = $('#datagrid').mmGrid("row",idx);
	var id = row.id;
	var quartzEnable = '1';
	if(row.quartzEnable=='1'){
		quartzEnable = '0';
	}else{
		quartzEnable = '1';
	}
	$.ajax({
		url:'quartzController/setEnable.do',
		data:{id:id,quartzEnable:quartzEnable},
		success:function(data){
			$('#query_link').click();
		}
	});

}